package com.wlscale.roadsystem.wiget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.inputmethodservice.KeyboardView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.wlscale.roadsystem.R;

public class KeyboardPopupWindow extends PopupWindow {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 弹出框布局
     */
    private View mContentView;
    /**
     *
     */
    private EditText mEditText;
    /**
     *
     */
    private KeyboardUtil keyboardUtil;

    private KeyboardView mKeyboardView;

    /**
     * 点击事件
     */
    private ReslutOnClickLister reslutOnClickLister;

    public KeyboardPopupWindow(Context context, EditText mEditText, ReslutOnClickLister reslutOnClickLister) {
        mContext = context;
        this.mEditText = mEditText;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContentView = inflater.inflate(R.layout.pop_keyboard, null);
        mKeyboardView = (KeyboardView) mContentView.findViewById(R.id.keyboard_view);
        this.reslutOnClickLister = reslutOnClickLister;
        initView();
    }

    private void initView() {
        //设置PopupWindow的View
        this.setContentView(mContentView);
        //设置弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置弹出窗体可点击
        this.setFocusable(true);
        //设置弹出窗体动画效果
//        this.setAnimationStyle(R.style.myTranslate);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.keyboard_anim);
        setBackgroundAlpha((Activity) mContext, 1.0f);
        mContentView.setOnTouchListener(new View.OnTouchListener() {// 如果触摸位置在窗口外面则销毁

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                int height = mContentView.findViewById(R.id.keyboard_view).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        initKeyBoad();
    }

    private void initKeyBoad() {

    }


    public void show(View view) {
        showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        if (keyboardUtil == null) {
            keyboardUtil = new KeyboardUtil((Activity) mContext, this, mKeyboardView, mEditText);
            keyboardUtil.hideSoftInputMethod();
            keyboardUtil.showKeyboard();
        } else {
            keyboardUtil.showKeyboard();
        }


    }

    //    @Override
//    public void showAsDropDown(View anchor) {
//        if (Build.VERSION.SDK_INT >= 24) {
//            Rect rect = new Rect();
//            anchor.getGlobalVisibleRect(rect);
//            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
//            setHeight(h);
//        }
//
//        super.showAsDropDown(anchor);
//
//    }
    public void dismissPop() {
        setBackgroundAlpha((Activity) mContext, 1.0f);
        super.dismiss();
    }

    @Override
    public void dismiss() {
        setBackgroundAlpha((Activity) mContext, 1.0f);
        if (null != keyboardUtil) {
            keyboardUtil.hideKeyboard();
        }

        super.dismiss();
    }

    /**
     * 设置页面的透明度
     *
     * @param bgAlpha 1表示不透明
     */
    private static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            //不移除该Fl ag的话,在有视频的页面上的视频会出现黑屏的bug
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            //此行代码主要是解决在华为手机上半透明效果无效的bug
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        activity.getWindow().setAttributes(lp);
    }

    public interface ReslutOnClickLister {
    }
}
